package com.zidongxiangxi.lock.zookeeper;

import com.zidongxiangxi.lock.core.DistributedLock;
import com.zidongxiangxi.lock.core.DistributedLockFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;

/**
 * This is Description
 *
 * @author chenxudong
 * @date 2021/12/30
 */
public class ZookeeperDistributedLockFactory implements DistributedLockFactory {
    private final String application;
    private final CuratorFramework framework;

    public ZookeeperDistributedLockFactory(String application, CuratorFramework framework) {
        this.application = application;
        this.framework = framework;
    }

    @Override
    public DistributedLock createLock(String lockName) {
        InterProcessMutex lock = new InterProcessMutex(framework, application + "/" + lockName);
        return new ZookeeperDistributedLock(lock);
    }
}
